package io.renren.modules.sys.controller;

import java.util.*;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.ProductAnalysisStatisticsEntity;
import io.renren.modules.sys.entity.ProductAnalysisEntity;
import io.renren.modules.sys.entity.ProductVOEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.ProductEntity;
import io.renren.modules.sys.service.ProductService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-03-19 15:58:27
 */
@RestController
@RequestMapping("sys/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:product:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productService.getProductList(params);

        return R.ok().put("page", page);
    }

    /**
     * 下架货架商品列表
     */
    @RequestMapping("/lowerlist")
    @RequiresPermissions("sys:product:lowerlist")
    public R lowerlist(@RequestParam Map<String, Object> params){
        PageUtils page = productService.getLowerProductList(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/getAnalysisList")
    @RequiresPermissions("sys:product:getAnalysisList")
    public R getAnalysisList(@RequestParam Map<String, Object> params){
        List<ProductAnalysisEntity> list = productService.getProductAnalysis(params);

        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:product:info")
    public R info(@PathVariable("id") Integer id){
        ProductEntity product = productService.getById(id);

        return R.ok().put("product", product);
    }

    /**
     * 下架货架对应的指定商品信息
     */
    @RequestMapping("/lowerinfo")
    @RequiresPermissions("sys:product:lowerinfo")
    public R lowerinfo(@RequestParam Map<String, Object> params){
        ProductVOEntity product = productService.getLowerProductInfo(params);

        return R.ok().put("product", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:product:save")
    public R save(@RequestBody ProductEntity product){
        if(StringUtils.isBlank(product.getBarcode())){
            StringBuilder str=new StringBuilder();//定义变长字符串
            Random random=new Random();
            //随机生成数字，并添加到字符串
            for(int i=0;i<9;i++){
                str.append(random.nextInt(10));
            }
            product.setBarcode("0000"+str);
        }
        product.setCreatetime(new Date());
        productService.save(product);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:product:update")
    public R update(@RequestBody ProductEntity product){
        ValidatorUtils.validateEntity(product);
        productService.updateById(product);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("sys:product:delete")
    public R delete(@PathVariable("id") Integer id){
        productService.removeById(id);

        return R.ok();
    }

    /**
     * 统计信息
     */
    @RequestMapping("/infoStatistics")
    @RequiresPermissions("sys:product:infoStatistics")
    public R infoStatistics(@RequestParam Map<String, Object> params){
        ProductAnalysisStatisticsEntity product = productService.getStatistics(params);

        return R.ok().put("product", product);
    }

}
